package org.apache.flink.kubernetes.kubeclient;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;

import static org.apache.flink.util.Preconditions.checkNotNull;

/**
 * A collection of variables that composes a ResourceConsumer/TaskManager Pod. This can include the Pod,
 * the main Container, and the InitContainer, etc.
 */
public class ComponentPod {

    private final Pod podWithoutMainContainer;

    private final Container mainContainer;

    public ComponentPod(Pod podWithoutMainContainer, Container mainContainer) {
        this.podWithoutMainContainer = podWithoutMainContainer;
        this.mainContainer = mainContainer;
    }

    public Pod getPodWithoutMainContainer() {
        return podWithoutMainContainer;
    }

    public Container getMainContainer() {
        return mainContainer;
    }

    public ComponentPod copy() {
        return new ComponentPod(
                new PodBuilder(this.getPodWithoutMainContainer()).build(),
                new ContainerBuilder(this.getMainContainer()).build());
    }

    /** Builder for creating a {@link ComponentPod}. */
    public static class Builder {

        private Pod podWithoutMainContainer;
        private Container mainContainer;

        public Builder() {
            this.podWithoutMainContainer =
                    new PodBuilder()
                            .withNewMetadata()
                            .endMetadata()
                            .withNewSpec()
                            .endSpec()
                            .build();

            this.mainContainer = new ContainerBuilder().build();
        }

        public Builder(ComponentPod compoentPod) {
            checkNotNull(compoentPod);
            this.podWithoutMainContainer = checkNotNull(compoentPod.getPodWithoutMainContainer());
            this.mainContainer = checkNotNull(compoentPod.getMainContainer());
        }

        public Builder withPod(Pod pod) {
            this.podWithoutMainContainer = checkNotNull(pod);
            return this;
        }

        public Builder withMainContainer(Container mainContainer) {
            this.mainContainer = checkNotNull(mainContainer);
            return this;
        }

        public ComponentPod build() {
            return new ComponentPod(this.podWithoutMainContainer, this.mainContainer);
        }
    }
}
